package com.ruoyi.graduate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.graduate.domain.entity.MsDelog;
import com.ruoyi.graduate.domain.param.MsDelogAddParam;
import com.ruoyi.graduate.domain.param.MsDelogPageListParam;
import com.ruoyi.graduate.domain.vo.MsDelogPageVo;
import com.ruoyi.graduate.mapper.MsDelogMapper;
import com.ruoyi.graduate.mapping.MsDelogMapping;
import com.ruoyi.graduate.service.IMsDelogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class IMsDelogServiceImpl extends ServiceImpl<MsDelogMapper, MsDelog>
implements IMsDelogService {

    @Autowired
    private MsDelogMapper msDelogMapper;


    @Override
    public AjaxResult listVo(Long customerId) {
        QueryWrapper<MsDelog> qw = new QueryWrapper<>();
// 条件1：当前用户的所有记录（无论is_all是否为1）
        qw.eq("customer_id", customerId)
                // 条件2：所有is_all为1的记录（无论customer_id是谁）
                .or()
                .eq("is_all", 1);
// 按创建时间倒序
        qw.orderByDesc("create_time");

        List list = msDelogMapper.selectList(qw);

        List list1 = MsDelogMapping.INSTANCE.listVo(list);

        return AjaxResult.success(list1);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult add(MsDelogAddParam param) {

        MsDelog add = MsDelogMapping.INSTANCE.add(param);

        int insert = msDelogMapper.insert(add);

        return insert == 1 ? AjaxResult.success() : AjaxResult.error();
    }

    @Override
    public PageInfo pageList(MsDelogPageListParam param) {

        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        QueryWrapper<MsDelog> qw = new QueryWrapper<>();
        qw.orderByDesc("create_time");
        qw.eq("is_all", 1);
        qw.like(!StringUtils.isEmpty(param.getTitle()),"title", param.getTitle());

        List<MsDelog> msDelogs = msDelogMapper.selectList(qw);

        List<MsDelogPageVo> msDelogPageVos = MsDelogMapping.INSTANCE.pageList(msDelogs);

        return new PageInfo(msDelogPageVos);
    }
}
